<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Ocr;
use App\Models\OcrBlood;
use App\Models\OcrThen;
use App\Models\OcrUrine;
use Illuminate\Http\Request;

class OcrController extends Controller
{
    public function index(Request $request)
    {
        $searchData = $request->post();

        $model = Ocr::orderBy('ocr.id', 'DESC')
            ->leftJoin('users', 'users.id', '=', 'ocr.user_id')
            ->leftJoin('groups', 'groups.id', '=', 'ocr.group_id')
            ->select(['ocr.*', 'users.nickname', 'groups.name as groups_name']);

//        if ($nickname = $searchData['nickname'] ?? '') {
//            $model->where('nickname', 'like', "%{$nickname}%");
//        }
//        if ($name = $searchData['name'] ?? '') {
//            $model->where('groups.name', 'like', "%{$name}%");
//        }
        $result = $model->paginate(config('app.limit'));

        $app_url = config('app.url');
        foreach ($result->items() as $item) {
            $item->img = $app_url . $item->img;
            $item->createdAt = $item->created_at->format('Y-m-d H:i:s');
        }

        return $this->writeJson(0, [
            'data' => $result->items(),
            'count' => $result->total()
        ]);
    }

    // 化验单详情
    public function detail(Request $request)
    {
        $pro_name = $request->post('pro_name');
        $id = $request->post('id');

        if ($pro_name == '血常规') {
            $model = OcrBlood::whereOcrId($id)->first();
        } elseif ($pro_name == '便常规') {
            $model = OcrThen::whereOcrId($id)->first();
        } elseif ($pro_name == '尿常规') {
            $model = OcrUrine::whereOcrId($id)->first();
        }

        return $this->writeJson(0, [
            'data' => $model ?? []
        ]);
    }
}
